package com.vca.admin.controller;

import com.vca.common.model.course.Course;
import com.vca.common.page.CommonPage;
import com.vca.common.request.*;
import com.vca.common.response.*;
import com.vca.common.response.sf.ExpressResponse;
import com.vca.common.response.sf.SendResponse;
import com.vca.common.result.CommonResult;
import com.vca.common.vo.CoursePackageCancelVo;
import com.vca.common.vo.ExpressSheetVo;
import com.vca.service.service.StoreOrderService;
import com.vca.service.service.StoreOrderVerification;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/store/order")
@Api(tags = "订单") //配合swagger使用
public class StoreOrderController {

    @Autowired
    private StoreOrderService storeOrderService;

    @Autowired
    private StoreOrderVerification storeOrderVerification;

    /**
     * 分页显示订单表
     *  @param request          搜索条件
     * @param pageParamRequest 分页参数
     */
    @PreAuthorize("hasAuthority('admin:order:list')")
    @ApiOperation(value = "分页列表") //配合swagger使用
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<StoreOrderDetailResponse>> getList(@Validated StoreOrderSearchRequest request, @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(storeOrderService.getAdminList(request, pageParamRequest));
    }

    @PreAuthorize("hasAuthority('admin:order:list')")
    @RequestMapping(value = "/getCoursesList", method = RequestMethod.GET)
    public CommonResult<List<Course>> getCoursesList(@Validated StoreOrderSearchRequest request){
        return CommonResult.success(storeOrderService.getCoursesList(request));
    }




    /**
     * 获取订单各状态数量
     */
    @PreAuthorize("hasAuthority('admin:order:status:num')")
    @ApiOperation(value = "获取订单各状态数量")
    @RequestMapping(value = "/status/num", method = RequestMethod.GET)
    public CommonResult<StoreOrderCountItemResponse> getOrderStatusNum(
            @RequestParam(value = "orderNo") String orderNo,
            @RequestParam(value = "dateLimit", defaultValue = "") String dateLimit,
            @RequestParam(value = "type", defaultValue = "5") @Range(min = 0, max = 5, message = "未知的订单类型") Integer type,
            @RequestParam(value = "productName") String productName,
            @RequestParam(value = "lecturerPhone") String lecturerPhone,
            @RequestParam(value = "realName") String realName,
            @RequestParam(value = "schedulingDate") String schedulingDate) {
        return CommonResult.success(storeOrderService.getOrderStatusNum(dateLimit, type, productName, lecturerPhone, realName, schedulingDate, orderNo));
    }

    /**
     * 获取订单统计数据
     */
    @PreAuthorize("hasAuthority('admin:order:list:data')")
    @ApiOperation(value = "获取订单统计数据")
    @RequestMapping(value = "/list/data", method = RequestMethod.GET)
    public CommonResult<StoreOrderTopItemResponse> getOrderData(@RequestParam(value = "dateLimit", defaultValue = "")String dateLimit) {
        return CommonResult.success(storeOrderService.getOrderData(dateLimit));
    }


//    /**
//     * 订单删除
//     */
//    @PreAuthorize("hasAuthority('admin:order:delete')")
//    @ApiOperation(value = "订单删除")
//    @RequestMapping(value = "/delete", method = RequestMethod.GET)
//    public CommonResult<String> delete(@RequestParam(value = "orderNo") String orderNo) {
//        if (storeOrderService.delete(orderNo)) {
//            return CommonResult.success();
//        } else {
//            return CommonResult.failed();
//        }
//    }

    /**
     * 备注订单
     */
    @PreAuthorize("hasAuthority('admin:order:mark')")
    @ApiOperation(value = "备注")
    @RequestMapping(value = "/mark", method = RequestMethod.POST)
    public CommonResult<String> mark(@RequestParam String orderNo, @RequestParam String mark) {
        if (storeOrderService.mark(orderNo, mark)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改订单(改价)
     */
    @PreAuthorize("hasAuthority('admin:order:update:price')")
    @ApiOperation(value = "修改订单(改价)")
    @RequestMapping(value = "/update/price", method = RequestMethod.POST)
    public CommonResult<String> updatePrice(@RequestBody @Validated StoreOrderUpdatePriceRequest request) {
        if (storeOrderService.updatePrice(request)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('admin:order:cancel')")
    @ApiOperation(value = "订单取消")
    @RequestMapping(value = "/order/cancel", method = RequestMethod.POST)
    public CommonResult orderCancel( @RequestParam(value = "orderNo") String orderNo) {
        return storeOrderService.orderCancel(orderNo);
    }

    @PreAuthorize("hasAuthority('admin:order:cancel')")
    @ApiOperation(value = "套课订单取消")
    @RequestMapping(value = "/order/coursePackage/cancel", method = RequestMethod.POST)
    public CommonResult orderCoursePackageCancel( @RequestBody @Validated CoursePackageCancelVo request) {
        return storeOrderService.orderCoursePackageCancel(request);
    }

    @PreAuthorize("hasAuthority('admin:order:cancel')")
    @ApiOperation(value = "套课订单详情")
    @RequestMapping(value = "/order/coursePackage/info", method = RequestMethod.GET)
    public CommonResult orderCoursePackageInfo( @RequestParam(value = "id") Integer id) {
        return CommonResult.success(storeOrderService.orderCoursePackageInfo(id));
    }


    /**
     * 订单详情
     */
    @PreAuthorize("hasAuthority('admin:order:info')")
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<StoreOrderInfoResponse> info(@RequestParam(value = "id") Integer id) {
        return CommonResult.success(storeOrderService.info(id));
    }

    /**
     * 发送货
     */
    @PreAuthorize("hasAuthority('admin:order:send')")
    @ApiOperation(value = "发送货")
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public CommonResult<SendResponse> send(@RequestBody @Validated StoreOrderSendRequest request) {
            return CommonResult.success(storeOrderService.send(request));
    }

    /**
     * 电子面单打印
     */
    @PreAuthorize("hasAuthority('admin:order:print')")
    @ApiOperation(value = "电子面单打印")
    @RequestMapping(value = "/print", method = RequestMethod.POST)
    public CommonResult<ExpressResponse> print(Integer orderId) {
        return CommonResult.success(storeOrderService.getExpress(orderId));
    }

    /**
     * 退款处理信息
     */
    @PreAuthorize("hasAuthority('admin:order:refund:info')")
    @ApiOperation(value = "退款处理信息")
    @RequestMapping(value = "/refund/info", method = RequestMethod.GET)
    public CommonResult<OrderRefundInfoResponse> RefundInfo(@RequestParam(value = "orderNo") String orderNo) {
        return CommonResult.success(storeOrderService.RefundInfo(orderNo));
    }

    /**
     * 退款处理
     */
    @PreAuthorize("hasAuthority('admin:order:refund:processing')")
    @ApiOperation(value = "退款处理")
    @RequestMapping(value = "/refund/processing", method = RequestMethod.POST)
    public CommonResult<Boolean> refundProcessing(@RequestBody OrderRefundProcessingRequest request) {
        return CommonResult.success(storeOrderService.refundProcessing(request));
    }


    /**
     * 退款
     */
    @PreAuthorize("hasAuthority('admin:order:refund')")
    @ApiOperation(value = "退款")
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public CommonResult<Boolean> refund(@RequestBody OrderRefunRequest request) {
        return CommonResult.success(storeOrderService.refund(request));
    }

    /**
     * 拒绝退款
     */
    @PreAuthorize("hasAuthority('admin:order:refund:refuse')")
    @ApiOperation(value = "拒绝退款")
    @RequestMapping(value = "/refund/refuse", method = RequestMethod.GET)
    public CommonResult<Object> refundRefuse(@RequestParam String orderNo, @RequestParam String reason) {
        if (storeOrderService.refundRefuse(orderNo, reason)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    /**
     * 核销订单头部数据
     *
     * @author stivepeim
     * @since 2020-08-29
     */
    @PreAuthorize("hasAuthority('admin:order:statistics')")
    @ApiOperation(value = "核销订单头部数据")
    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
    public CommonResult<StoreStaffTopDetail> getStatistics() {
        return CommonResult.success(storeOrderVerification.getOrderVerificationData());
    }



    /**
     * 核销码核销订单
     *
     * @author stivepeim
     * @since 2020-09-01
     */
    @PreAuthorize("hasAuthority('admin:order:write:update')")
    @ApiOperation(value = "核销订单")
    @RequestMapping(value = "/writeUpdate/{id}", method = RequestMethod.POST)
    public CommonResult<Object> verificationOrder(@PathVariable Integer id) {
        return CommonResult.success(storeOrderVerification.verificationOrderByCode(id));
    }

    /**
     * 核销码查询待核销订单
     *
     * @author stivepeim
     * @since 2020-09-01
     */
    @PreAuthorize("hasAuthority('admin:order:write:confirm')")
    @ApiOperation(value = "查询待核销订单")
    @RequestMapping(value = "/writeConfirm", method = RequestMethod.GET)
    public CommonResult<StoreOrderVerificationConfirmResponse> verificationConfirmOrder( @Validated OrderWriteOffRequest request, @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(storeOrderVerification.getVerificationOrderByCode(request,pageParamRequest));
    }


    @PreAuthorize("hasAuthority('admin:order:write:confirm')")
    @ApiOperation(value = "核销记录")
    @RequestMapping(value = "/WriteOffRecord", method = RequestMethod.GET)
    public CommonResult<CommonPage<WriteOffRecordResponse>> WriteOffRecordInfo(@Validated WriteOffRecordRequest request, @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(storeOrderVerification.getWriteOffRecordList(request,pageParamRequest));
    }

    /**
     * 订单统计详情
     *
     * @author stivepeim
     * @since 2020-09-01
     */
    @PreAuthorize("hasAuthority('admin:order:time')")
    @ApiOperation(value = "订单统计详情")
    @RequestMapping(value = "/time", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dateLimit", value = "today,yesterday,lately7,lately30,month,year,/yyyy-MM-dd hh:mm:ss,yyyy-MM-dd hh:mm:ss/",
                    dataType = "String", required = true),
            @ApiImplicitParam(name = "type", value = "1=price 2=order", required = true)
    })
    public CommonResult<Object> statisticsOrderTime(@RequestParam String dateLimit,
                                                    @RequestParam Integer type) {
        return CommonResult.success(storeOrderService.orderStatisticsByTime(dateLimit, type));
    }

    /**
     * 获取面单默认配置信息
     */
    @PreAuthorize("hasAuthority('admin:order:sheet:info')")
    @ApiOperation(value = "获取面单默认配置信息")
    @RequestMapping(value = "/sheet/info", method = RequestMethod.GET)
    public CommonResult<ExpressSheetVo> getDeliveryInfo() {
        return CommonResult.success(storeOrderService.getDeliveryInfo());
    }

}



